package com.runshu.runshuwulian.ui.viewhelper;

import com.runshu.runshuwulian.MApplication;
import com.runshu.runshuwulian.base.net.BaseObserver;
import com.runshu.runshuwulian.base.net.RetrofitFactory;
import com.runshu.runshuwulian.bean.BaseBean;
import com.runshu.runshuwulian.bean.LoginBean;
import com.runshu.runshuwulian.bean.NullBean;
import com.runshu.runshuwulian.ui.activity.LoginByPhoneActivity;

public class LoginByPhoneViewHelper {
    LoginByPhoneActivity activity;

    public LoginByPhoneViewHelper(LoginByPhoneActivity activity) {
        this.activity = activity;
    }

    public void sendERcode(String phoneStr) {
        activity.api().getCaptCha( phoneStr ).compose( RetrofitFactory.threadTransformer() )
                .subscribe( new BaseObserver<NullBean>() {
                    @Override
                    protected void onSuccees(BaseBean<NullBean> response) throws Exception {
                        activity.showShortToast( "发送验证码成功" );
                    }

                    @Override
                    protected void onFailure(Throwable e, boolean isNetWorkError) throws Exception {
                        activity.showShortToast( e.getMessage() );
                        activity.setYZMBtState( LoginByPhoneActivity.YZM_STATE_GETTING_OVERTIME );
                    }
                } );
    }

    public void login(String phoneStr, String erCodeStr) {
        activity.api().loginByPhone( phoneStr, null, erCodeStr ).compose( RetrofitFactory.threadTransformer() )
                .subscribe( new BaseObserver<LoginBean>() {
                    @Override
                    protected void onSuccees(BaseBean<LoginBean> response) throws Exception {
                        LoginBean loginBean = response.getData();
                        MApplication.instance.setLoginInfo( loginBean );
                        activity.dissmisProgressDialog();
                        activity.onLoginSuccess();
                    }

                    @Override
                    protected void onFailure(Throwable e, boolean isNetWorkError) throws Exception {
                        activity.dissmisProgressDialog();
                        activity.showShortToast( e.getMessage() );
                    }
                } );
    }
}
